package com.bwda.dsrs.filecenter.service.impl;


import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.filecenter.api.OrgServiceApi;
import com.bwda.dsrs.filecenter.api.clientappversion.datadict.DatadictQueryForm;
import com.bwda.dsrs.filecenter.api.clientappversion.datadict.DatadictVo;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.api.DatadictUpdateForm;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.vo.processstrategy.ProcessStrategyTypeVo;
import com.bwda.dsrs.filecenter.domain.vo.processstrategy.ProcessStrategyVo;
import com.bwda.dsrs.filecenter.service.ProcessStrategyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 策略管理--流程策略--服务层实现类
 *
 * @author zhangyinmei
 * @date 2019年4月27日
 */
@Service
public class ProcessStrategyServiceImpl implements ProcessStrategyService {

    private static final Logger logger = LoggerFactory.getLogger(ProcessStrategyServiceImpl.class);

    @Resource
    private OrgServiceApi orgServiceApi;

    @Override
    public List<ProcessStrategyVo> queryDataDictList() throws BusinessServiceException {
        List<ProcessStrategyVo> result = new ArrayList<ProcessStrategyVo>();
        try{
            //定义查询参数实体
            DatadictQueryForm datadictQueryForm = new DatadictQueryForm();
            //设置数据字典类别code为流程策略类型
            datadictQueryForm.setDataDictTypeCode(BusinessConstants.PROCESS_STRATEGY_TYPE);
            logger.info("开始查询“流程策略类型”字典数据");
            //调用字典表接口-查询流程策略类型
            ResponseEntity<CentreListResponse<DatadictVo>> responseEntityToProStrategyType = orgServiceApi.queryDatadictList(datadictQueryForm);
            logger.info("结束查询“流程策略类型”字典数据");
            if(responseEntityToProStrategyType.getData() == null || responseEntityToProStrategyType.getData().getDataList() == null){
                return result;
            }
            List<DatadictVo> dataDictVoToProStrategyTypeList = responseEntityToProStrategyType.getData().getDataList();
            //用于存放策略类型集合
            List<ProcessStrategyTypeVo> processStrategyTypeVoList = new ArrayList<ProcessStrategyTypeVo>();
            if(dataDictVoToProStrategyTypeList != null && !dataDictVoToProStrategyTypeList.isEmpty()){
                //遍历字典接口返回数据
                for(DatadictVo psTypeVo : dataDictVoToProStrategyTypeList){
                    ProcessStrategyTypeVo processStrategyTypeVo = new ProcessStrategyTypeVo();
                    processStrategyTypeVo.setDataDictName(psTypeVo.getDataDictName());
                    processStrategyTypeVo.setDataDictValue(psTypeVo.getDataDictValue());
                    processStrategyTypeVoList.add(processStrategyTypeVo);
                }
            }
            //设置数据字典类别code为操作类型
            datadictQueryForm.setDataDictTypeCode(BusinessConstants.OPERATE_TYPE);
            //调用字典数据接口
            logger.info("开始查询“操作类型”字典数据");
            //调用字典表接口-查询操作类型
            ResponseEntity<CentreListResponse<DatadictVo>> responseEntityToOptType = orgServiceApi.queryDatadictList(datadictQueryForm);
            if(responseEntityToOptType.getData() == null || responseEntityToOptType.getData().getDataList() == null){
                return result;
            }
            logger.info("结束查询“操作类型”字典数据");
            List<DatadictVo> dataDictVoToOptTypeList = responseEntityToOptType.getData().getDataList();
            if(dataDictVoToOptTypeList != null && !dataDictVoToOptTypeList.isEmpty()) {
                //遍历字典接口返回数据
                for(DatadictVo datadictVo : dataDictVoToOptTypeList){
                    ProcessStrategyVo processStrategyVo = new ProcessStrategyVo();
                    processStrategyVo.setDataDictId(datadictVo.getId());
                    processStrategyVo.setDataDictTypeCode(datadictVo.getDataDictTypeCode());
                    processStrategyVo.setDataDictCode(datadictVo.getDataDictCode());
                    processStrategyVo.setDataDictName(datadictVo.getDataDictName());
                    processStrategyVo.setDataDictValue(datadictVo.getDataDictValue());
                    //先匹配出数据库设置了哪个流程策略
                    if(processStrategyTypeVoList != null && !processStrategyTypeVoList.isEmpty()){
                        List<ProcessStrategyTypeVo> pstVoList = new ArrayList<ProcessStrategyTypeVo>();
                        for(ProcessStrategyTypeVo pstVo : processStrategyTypeVoList){
                            ProcessStrategyTypeVo newPstVo = new ProcessStrategyTypeVo();
                            newPstVo.setCheckedFlag(false);
                            newPstVo.setDataDictName(pstVo.getDataDictName());
                            newPstVo.setDataDictValue(pstVo.getDataDictValue());
                            pstVoList.add(newPstVo);
                        }
                        for(ProcessStrategyTypeVo pstVo : pstVoList){
                            if(datadictVo.getDataDictValue().equals(pstVo.getDataDictValue())){
                                pstVo.setCheckedFlag(true);
                                break;
                            }
                        }
                        //设置流程策略类型
                        processStrategyVo.setProcessStrategyTypeVoList(pstVoList);
                    }
                    result.add(processStrategyVo);
                }
            }
        }catch(Exception e){
            logger.info("字典工具类调用失败！");
            throw new BusinessServiceException("字典工具类调用失败！");
        }
        return result;
    }

    @Override
    public void updateDataDict(DatadictUpdateForm datadictUpdateForm) throws BusinessServiceException {
        try{
            //需要处理一下，因为前端只能传dataDictId 和 dataDictName
            //1.根据dataDictName查询出 操作类型字典数据列表中字典数据为dataDictName 的 dataDictValue
            //定义查询参数实体
            DatadictQueryForm datadictQueryForm = new DatadictQueryForm();
            //设置数据字典类别code为操作类型
            datadictQueryForm.setDataDictTypeCode(BusinessConstants.PROCESS_STRATEGY_TYPE);
            datadictQueryForm.setDataDictName(datadictUpdateForm.getDataDictName());
            //调用字典数据接口
            logger.info("开始查询“操作类型”字典数据");
            //调用字典表接口-查询操作类型
            ResponseEntity<CentreListResponse<DatadictVo>> responseEntityToOptType = orgServiceApi.queryDatadictList(datadictQueryForm);
            logger.info("结束查询“操作类型”字典数据");
            if(responseEntityToOptType.getData() == null ||responseEntityToOptType.getData().getDataList() == null){
                return;
            }
            List<DatadictVo> dataDictVoToOptTypeList = responseEntityToOptType.getData().getDataList();
            if(dataDictVoToOptTypeList != null && !dataDictVoToOptTypeList.isEmpty()) {
                //遍历字典接口返回数据,正确的应该只有一条数据
                datadictUpdateForm.setDataDictValue(dataDictVoToOptTypeList.get(BusinessConstants.NumberConstant.ZERO).getDataDictValue());
                //2.根据dataDictId确定需要更新的记录
                //设置字典名称为null,不更新字典名称。
                datadictUpdateForm.setDataDictName(null);
                logger.info("开始更新“操作类型”字典数据");
                //更新流程策略字典数据
                orgServiceApi.updateDatadict(datadictUpdateForm);
                logger.info("结束更新“操作类型”字典数据");
            }
        }catch(Exception e){
            logger.info("字典工具类调用失败！");
            throw new BusinessServiceException("字典工具类调用失败！");
        }
    }
}